package cn.neusoft.service.impl;

import cn.neusoft.mapper.OrderDetailMapper;
import cn.neusoft.mapper.OrderMapper;
import cn.neusoft.pojo.Order;
import cn.neusoft.pojo.OrderDetail;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.OrderQuery;
import cn.neusoft.service.IOrderService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service

public class OrderService implements IOrderService {
	@Autowired
	private OrderMapper orderMapper;
	//在添加订单的时候，还得调用订单明细这个mapper的对象
	@Autowired
	private OrderDetailMapper orderDetailMapper;
	@Transactional
	public ResultBean add(Order order) throws Exception {

		System.out.println("订单----"+order);

		if(order==null ||
		order.getBusinessId()==null || order.getBusinessId()==0 ||
		order.getUserId()==null || order.getUserId()==0 ||
		order.getAddressId()==null || order.getAddressId()==0) {
			return ResultBean.ERROR("请求参数为空");
		}
		//当进行完参数检验的时候，之后就开始进行为这个order对象设置参数
		order.setOrderState(0);
		order.setOrderDate(new Date());
		//设置orderNumber
		order.setOrderNumber(UUID.randomUUID().toString().replaceAll("-","").substring(0,10));
		//设置这个订单的总价
		//这里就需要在一个订单对象里面携带一个订单详情的集合
		//总价不存在
		if(order.getOrderTotal()==null) {
			List<OrderDetail> details = order.getDetailList();
			double allPrice=0;
			for (OrderDetail detail: details) {
				System.out.println("进行总价修改");
				allPrice+=detail.getFood().getFoodPrice()*detail.getQuantity();
			}
			order.setOrderTotal(allPrice);
		}
		int count=orderMapper.add(order);
		//添加成功之后还得继续添加订单详情
		if(count > 0) {
			//为每个订单详情进行设置订单编号，应该在测试的时候没有进行设置。
			List<OrderDetail> details = order.getDetailList();
			for (OrderDetail detail: details) {
				detail.setOrderId(order.getOrderId());
			}
			//批量添加订单详情
			int i=orderDetailMapper.addBatch(order.getDetailList());
			//判断是否添加订单详情成功
			if(i==order.getDetailList().size()) {
				System.out.println("添加订单详情成功");
				return ResultBean.SUCCESS();
			}
		}
		return ResultBean.ERROR("添加订单失败");
	}
	@Transactional
	public ResultBean update(Order order) throws Exception {
		//在修改一个订单的时候，大部分都是修改订单状态和订单的一个支付状态
		//所以其他剩余部分要进行参数检验
		if (order==null ||
				order.getOrderId()==null || order.getOrderId()==0 ||
				order.getUserId()==null || order.getUserId()==0 ||
				order.getBusinessId()==null || order.getBusinessId()==0 ||
				order.getAddressId()==null || order.getAddressId()==0  ||
				order.getOrderDate()==null ||
				order.getOrderTotal()==null || order.getOrderTotal()==0
		){
			return ResultBean.ERROR("请求参数为空值");
		}
		int count=orderMapper.update(order);
		return count>0?ResultBean.SUCCESS():ResultBean.ERROR("update is failed");
	}

	@Transactional
	public ResultBean delete(Integer id) throws Exception {
		if(id==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		int count=orderMapper.delete(id);
		if(count==0) {
			throw  new RuntimeException("删除订单信息失败");
		}
		//如果这里这个订单没有详情的话，会进行报错
		//解决办法，先根据这个订单先进行查询，如果查询有这个订单的详情之后，在进行删除，
		//否则服务端直接会抛出一个异常
		List<OrderDetail> list=orderDetailMapper.getCountByOrderId(id);
		if(list.size()==0) {
			//按道理来说的话，一个订单肯定有订单详情
			return ResultBean.ERROR("该订单没有订单详情，无法删除");
		} else {
			//当程序走到这个地步的话，也就不需要在进行判断了
			//肯定有订单的详情了
			int i=orderDetailMapper.deleteByOrderId(id);
			if(i==0 ) {
				throw new RuntimeException("删除订单详情失败");
			}
		}
		return ResultBean.SUCCESS();
	}
	@Transactional(readOnly = true)
	public ResultBean getById(Integer id) throws Exception {
		if(id==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		Order order = orderMapper.getById(id);
		return order!=null?ResultBean.SUCCESS(order):ResultBean.ERROR("getById is failed");
	}
	@Transactional(readOnly = true)
	public ResultBean getAll() throws Exception {
		List<Order>  list=orderMapper.getAll();
		return list!=null?ResultBean.SUCCESS(list):ResultBean.ERROR("getAll is failed");
	}
	@Transactional(readOnly = true)
	public ResultBean getByPage(BaseQuery baseQuery) throws Exception {
		PageBean<Order> pageBean=new PageBean<Order>();
		if(baseQuery==null) {
			baseQuery=new OrderQuery();
		}
		int count=orderMapper.getCount(baseQuery);
		pageBean.setTotalRows(count);
		//这里为什么不需要进行计算总页数和总记录数
		List<Order> list= orderMapper.getByPage(baseQuery);
		pageBean.setData(list);
		return ResultBean.SUCCESS(pageBean);
	}

	@Transactional
	public ResultBean batchDel(List<Integer> ids) throws Exception {
		if(ids==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		//这里直接复用单个删除的方法
		for ( Integer id: ids) {
			delete(id);
		}
		return ResultBean.SUCCESS();
	}

	@Override
	public ResultBean getDetailsByOrderId(Integer orderId) {
		if(orderId==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		List<OrderDetail> detailList = orderDetailMapper.getByOrderId(orderId);
		return ResultBean.SUCCESS(detailList);
	}

	@Override
	public ResultBean getOrderByUserId(Integer userId) {
		if(userId==null || userId==0) {
			return ResultBean.ERROR("请求参数为空");
		}
		List<Order> orderList = orderMapper.getOrderByUserId(userId);
		return ResultBean.SUCCESS(orderList);
	}
}
